<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title>后台登录</title>
    <link rel="stylesheet" type="text/css" href="/zhaojun/layui/css/layui.css"/>
    <link rel="stylesheet" type="text/css" href="/zhaojun/css/login.css"/>
</head>

<body>
<div class="m-login-bg">
    <div class="m-login">
        <h3>后台系统登录</h3>
        <div class="m-login-warp">
            <form class="layui-form">
                <div class="layui-form-item">
                    <input type="text" name="username" required lay-verify="username" placeholder="用户名" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <input type="password" name="password" required lay-verify="password" placeholder="密码" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <input type="text" name="verity" required lay-verify="required" placeholder="验证码" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-inline">
                        <img class="verifyImg" onclick="this.src=this.src+'?c='+Math.random();" src="/zhaojun/images/login/yzm.jpg"/>
                    </div>
                </div>
                <div class="layui-form-item m-login-btn">
                    <div class="layui-inline">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="login">登录</button>
                    </div>
                    <div class="layui-inline">
                        <button type="reset" class="layui-btn layui-btn-primary">取消</button>
                    </div>
                </div>
            </form>
        </div>
        <p class="copyright">Copyright zhaojun-cloud-service</p>
    </div>
</div>
<script src="/zhaojun/layui/layui.js" type="text/javascript" charset="utf-8"></script>
<script>
    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form,layer = layui.layer;
        var $ = layui.$;

        if(window.localStorage){
            //支持
        }else{
            layer.alert("浏览器不支持localStorage");
        }


        //自定义验证规则
        form.verify({
            username: function (value) {
                if (value.length < 5) {
                    return '用户名至少得5个字符';
                }
            },
            password: [/(.+){5,10}$/, '密码必须5到10位']

        });

        //监听提交
        form.on('submit(login)', function (data) {
            $.ajax({
                type:"post",    //请求方式
                async:true,    //是否异步
                url:"http://127.0.0.1:8031/zhaojun-service-sso/login",
                data:data.field,
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                success: function(data) {
                    if (data.code == '200'){
                        var storage=window.localStorage;
                        storage.setItem("userId",data.data.id);
                        layer.msg(data.data.msg);
                    }else{
                        layer.msg(data.data.msg);
                    }
                },
                error: function() {
                    layer.msg("登录失败");
                }
            });
            return false;
        });

    });
</script>
</body>
</html>